//
//  SDTestReportDeatilDetailList.m
//
//  Created by   on 2019/3/12
//  Copyright (c) 2019 __MyCompanyName__. All rights reserved.
//

#import "SDTestReportDeatilDetailList.h"
#import "SDTestReportDeatilProductList.h"


NSString *const kSDTestReportDeatilDetailListTitle = @"title";
NSString *const kSDTestReportDeatilDetailListProductList = @"productList";
NSString *const kSDTestReportDeatilDetailListResultFeature = @"resultFeature";
NSString *const kSDTestReportDeatilDetailListResultDesc = @"resultDesc";


@interface SDTestReportDeatilDetailList ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SDTestReportDeatilDetailList

@synthesize title = _title;
@synthesize productList = _productList;
@synthesize resultFeature = _resultFeature;
@synthesize resultDesc = _resultDesc;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.title = [self objectOrNilForKey:kSDTestReportDeatilDetailListTitle fromDictionary:dict];
    NSObject *receivedSDTestReportDeatilProductList = [dict objectForKey:kSDTestReportDeatilDetailListProductList];
    NSMutableArray *parsedSDTestReportDeatilProductList = [NSMutableArray array];
    
    if ([receivedSDTestReportDeatilProductList isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedSDTestReportDeatilProductList) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedSDTestReportDeatilProductList addObject:[SDTestReportDeatilProductList modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedSDTestReportDeatilProductList isKindOfClass:[NSDictionary class]]) {
       [parsedSDTestReportDeatilProductList addObject:[SDTestReportDeatilProductList modelObjectWithDictionary:(NSDictionary *)receivedSDTestReportDeatilProductList]];
    }

    self.productList = [NSArray arrayWithArray:parsedSDTestReportDeatilProductList];
            self.resultFeature = [self objectOrNilForKey:kSDTestReportDeatilDetailListResultFeature fromDictionary:dict];
            self.resultDesc = [self objectOrNilForKey:kSDTestReportDeatilDetailListResultDesc fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.title forKey:kSDTestReportDeatilDetailListTitle];
    NSMutableArray *tempArrayForProductList = [NSMutableArray array];
    
    for (NSObject *subArrayObject in self.productList) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForProductList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForProductList addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForProductList] forKey:kSDTestReportDeatilDetailListProductList];
    [mutableDict setValue:self.resultFeature forKey:kSDTestReportDeatilDetailListResultFeature];
    [mutableDict setValue:self.resultDesc forKey:kSDTestReportDeatilDetailListResultDesc];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.title = [aDecoder decodeObjectForKey:kSDTestReportDeatilDetailListTitle];
    self.productList = [aDecoder decodeObjectForKey:kSDTestReportDeatilDetailListProductList];
    self.resultFeature = [aDecoder decodeObjectForKey:kSDTestReportDeatilDetailListResultFeature];
    self.resultDesc = [aDecoder decodeObjectForKey:kSDTestReportDeatilDetailListResultDesc];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_title forKey:kSDTestReportDeatilDetailListTitle];
    [aCoder encodeObject:_productList forKey:kSDTestReportDeatilDetailListProductList];
    [aCoder encodeObject:_resultFeature forKey:kSDTestReportDeatilDetailListResultFeature];
    [aCoder encodeObject:_resultDesc forKey:kSDTestReportDeatilDetailListResultDesc];
}

- (id)copyWithZone:(NSZone *)zone {
    SDTestReportDeatilDetailList *copy = [[SDTestReportDeatilDetailList alloc] init];
    
    
    
    if (copy) {

        copy.title = [self.title copyWithZone:zone];
        copy.productList = [self.productList copyWithZone:zone];
        copy.resultFeature = [self.resultFeature copyWithZone:zone];
        copy.resultDesc = [self.resultDesc copyWithZone:zone];
    }
    
    return copy;
}


@end
